package com.bayleaf.common;

public class NumericUtil {

	    /**
	     * convBinary
	     * @param nDecimal
	     * @return String
	     */
		public static String convBinary(int nDecimal)
		{
			return Integer.toBinaryString(nDecimal);
		}

		/**
		 * convOctal
		 * @param nDecimal
		 * @return
		 */
		public static String convOctal(int nDecimal)
		{
			return Integer.toOctalString(nDecimal);
		}

		/**
		 * convHeximal
		 * @param nDecimal
		 * @return
		 */
		public static String convHeximal(int nDecimal)
		{
			return Integer.toHexString(nDecimal);
		}

		public static byte[] int2byte(int nValue)
		{
			byte[] bBytes = new byte[4];

			bBytes[0] = (byte)((nValue >> 24) & 0xff);
			bBytes[1] = (byte)((nValue >> 16) & 0xff);
			bBytes[2] = (byte)((nValue >> 8) & 0xff);
			bBytes[3] = (byte)(nValue);

			return bBytes;
		}

		public static int[] viewDecimalFormat(byte[] bBytes)
		{
			int[] nrgDecValue = new int[4];

			for (int i = 0; i < 4; i++)		{
				nrgDecValue[i] = (int)bBytes[i];

				if (nrgDecValue[i] < 0)	nrgDecValue[i] += 256;
			}

			return nrgDecValue;
		}

		public static String[] viewBinaryFormat(byte[] bBytes)
		{
			String[] szrgByteCode = new String[4];

			for (int i = 0; i < 4; i++)		{
				int nTempData = (int)bBytes[i];
				
				// 'byte' range = -128 ~ 127
				if (nTempData < 0)	nTempData += 256;

				szrgByteCode[i] = convBinary(nTempData);

				if (szrgByteCode[i].length() < 8)	{
					int nLen = szrgByteCode[i].length();
					while (nLen++ < 8)	{
						szrgByteCode[i] = "0" + szrgByteCode[i];
					}
				}
			}

			return szrgByteCode;
		}
		
		
		

		public static int byte2int(byte[] bValue)
		{
			return (int)((bValue[0] & 0xff) << 24) |
				   (int)((bValue[1] & 0xff) << 16) |
				   (int)((bValue[2] & 0xff) << 8) |
				   (int)(bValue[3] & 0xff);
		}

		public static void main(String[] args)
		{

			//	Test for... hard -_-

			int nSampleNum1 = 10;
			int nSampleNum2 = 2147483647;
			int nSampleNum3 = -2147483648;

			byte[] bBytesSample = null;
			String[] szrgBytes = null;
			int[] nrgData = null;

			System.out.println(" * Decimal : " + nSampleNum1 + ", " + nSampleNum2 + ", " + nSampleNum3);
			System.out.println(" * Binary : " + convBinary(nSampleNum1) + ", " + convBinary(nSampleNum2) + ", " + convBinary(nSampleNum3));
			System.out.println(" * Octal : " + convOctal(nSampleNum1) + ", " + convOctal(nSampleNum2) + ", " + convOctal(nSampleNum3));
			System.out.println(" * Heximal : " + convHeximal(nSampleNum1) + ", " + convHeximal(nSampleNum2) + ", " + convHeximal(nSampleNum3));

			System.out.println("\n * INTEGER --> BYTES : ");

			// int2byte ( nSampleNum1 )
			System.out.print("   > nSampleNum1 = ");
			bBytesSample = int2byte(nSampleNum1);
			nrgData = viewDecimalFormat(bBytesSample);
			szrgBytes = viewBinaryFormat(bBytesSample);
			for (int i = 0; i < 4; i++)		{
				System.out.print(nrgData[i] + " ");
			}
			System.out.print(" | ");
			for (int i = 0; i < 4; i++)		{
				System.out.print(szrgBytes[i] + " ");
			}
			System.out.println();
			bBytesSample = null;
			szrgBytes = null;

			// int2byte ( nSampleNum2 )
			System.out.print("   > nSampleNum2 = ");
			bBytesSample = int2byte(nSampleNum2);
			nrgData = viewDecimalFormat(bBytesSample);
			szrgBytes = viewBinaryFormat(bBytesSample);
			for (int i = 0; i < 4; i++)		{
				System.out.print(nrgData[i] + " ");
			}
			System.out.print(" | ");
			for (int i = 0; i < 4; i++)		{
				System.out.print(szrgBytes[i] + " ");
			}
			System.out.println();
			bBytesSample = null;
			szrgBytes = null;

			// int2byte ( nSampleNum3 )
			System.out.print("   > nSampleNum3 = ");
			bBytesSample = int2byte(nSampleNum3);
			nrgData = viewDecimalFormat(bBytesSample);
			szrgBytes = viewBinaryFormat(bBytesSample);
			for (int i = 0; i < 4; i++)		{
				System.out.print(nrgData[i] + " ");
			}
			System.out.print(" | ");
			for (int i = 0; i < 4; i++)		{
				System.out.print(szrgBytes[i] + " ");
			}
			System.out.println();
			bBytesSample = null;
			szrgBytes = null;

			System.out.println();
			
			//2
			for(int i =31; i>=0;i-=1){
				 System.out.printf("%d",(nSampleNum1>>i)&1);
				 if(i%8 == 0) System.out.print(" ");
				}
			System.out.println();
			for(int i =28; i>=0;i-=7){
				 System.out.printf("%d",(nSampleNum1>>i)&7);
				 if(i%3 == 0) System.out.print(" ");
				}
			System.out.println();
				for(int i=15; i>=0; i-=15){
				 System.out.printf("%d",(nSampleNum1>>i)&15);
				 if(i%4 == 0) System.out.print(" ");
				}
				

		}

}
